/**
 * 
 */
package game.kill.model;

import game.kill.enums.PlayerRoleEnum;
import game.kill.enums.PlayerStatusEnum;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;

/**
 * @author leiruiqi
 *
 */
public class GameRoomInfo {
	
	private Room room;
	
	private Map<Integer,RoomUser> roomUserMap;

	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}

	public Map<Integer, RoomUser> getRoomUserMap() {
		return roomUserMap;
	}

	public void setRoomUserMap(Map<Integer, RoomUser> roomUserMap) {
		this.roomUserMap = roomUserMap;
	}
	
	
	public List<RoomUser> findTeamPlayser(Integer playerNum){
		RoomUser roomUser = roomUserMap.get(playerNum);
		Integer role = roomUser.getRoleId();
		List<RoomUser> teamPlayers = Lists.newArrayList();
		for(RoomUser player: roomUserMap.values()){
			if(player.getRoleId() == role){
				teamPlayers.add(player);
			}
		}
		return teamPlayers;
		
	}
	
	public List<RoomUser> findTeamPlayserByRoleId(Integer roleId){
		
		List<RoomUser> teamPlayers = Lists.newArrayList();
		for(RoomUser player: roomUserMap.values()){
			if(player.getRoleId() == roleId){
				teamPlayers.add(player);
			}
		}
		return teamPlayers;
		
	}
	
	public boolean isAlivePeople(Integer playerNum){
		RoomUser roomUser = roomUserMap.get(playerNum);
		if(roomUser!=null && roomUser.getStatus()==PlayerStatusEnum.normal.getCode()){
			return true;
		}
		return false;
	}
	
	public RoomUser findByUserId(Long userId){
		for(RoomUser player: roomUserMap.values()){
			if(player.getUserId() == userId){
				return player;
			}
		}
		return null;
	}
	public List<RoomUser> findAllAlivePeople(){
		
		List<RoomUser> teamPlayers = Lists.newArrayList();
		for(RoomUser player: roomUserMap.values()){
			if(player.getStatus() == 0){
				teamPlayers.add(player);
			}
		}
		return teamPlayers;
		
	}

	public boolean isNeedStart(){
		if(roomUserMap.size() >= room.getPlayerNum()){
			return true;
		}
		return false;
	}
	public boolean isGameOver(){
		List<RoomUser> aliveKiller = Lists.newArrayList();
		List<RoomUser> alivePolice = Lists.newArrayList();
		List<RoomUser> alivePeople = Lists.newArrayList();
		for(RoomUser roomUser:roomUserMap.values()){
			if(roomUser.getStatus() == PlayerStatusEnum.normal.getCode()){
				if(roomUser.getRoleId()==PlayerRoleEnum.killer.getCode()){
					aliveKiller.add(roomUser);
				}
				else if(roomUser.getRoleId()==PlayerRoleEnum.police.getCode()){
					alivePolice.add(roomUser);
				}
				else if(roomUser.getRoleId()==PlayerRoleEnum.everyman.getCode()){
					alivePeople.add(roomUser);
				}
				
			}
			
			
			
		}
		if(aliveKiller.size()==0||alivePolice.size()==0||alivePeople.size()==0){
			return true;
		}
		return false;
	}
}
